import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:school_assistant_m/app/modules/course/views/course_view.dart';
import 'package:school_assistant_m/app/modules/homework/views/homework_view.dart';
import 'package:school_assistant_m/app/modules/mine/views/mine_view.dart';

import '../controllers/home_controller.dart';

class HomeView extends GetView<HomeController> {
  // 底部导航项目
  late final List<BottomNavigationBarItem> bottomTabs = [
    BottomNavigationBarItem(
      icon: Icon(Icons.home_rounded),
      activeIcon: Icon(Icons.home_rounded),
      label: '课程',
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.menu_book),
      activeIcon: Icon(Icons.menu_book),
      label: '作业',
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.person),
      activeIcon: Icon(Icons.person),
      label: '我的',
    ),
  ];

  // 底部导航
  Widget _getBottomNavigationBar() {
    return GetBuilder(
      init: controller,
      builder: (_) => BottomNavigationBar(
        items: bottomTabs,
        currentIndex: controller.currentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (index) => controller.handleNavBarTap(index),
        selectedItemColor: Color.fromARGB(255, 0, 137, 253),
        unselectedItemColor: Color(0xFF191A1E),
      ),
    );
  }

  // 内容页
  Widget _buildPageView() {
    return PageView(
      physics: NeverScrollableScrollPhysics(),
      children: <Widget>[
        CourseView(),
        HomeworkView(),
        MineView(),
      ],
      controller: controller.pageController,
      onPageChanged: (index) => controller.handleNavBarTap(index),
    );
  }

  @override
  Widget build(BuildContext context) {
    bool isPad = context.isTablet;
    return Scaffold(
      //导航栏组件
      bottomNavigationBar: _getBottomNavigationBar(),
      body: GetBuilder<HomeController>(
        init: controller,
        builder: (_){
          if(controller.isLoading){
            return Center(child: CircularProgressIndicator(),);
          } else{
            return _buildPageView();
          }
        },
      )
    );
  }
}
